﻿using System.Linq;
using System;
using System.Web;
using LINQExtensions;

namespace EasyLift.TMS.Web.Models
{
    public class DriverHRStatusRepository
    {
        private CoreBizDBEntities db = new CoreBizDBEntities();

        //Query Methods
        public IQueryable<DriverHRStatus> GetAllData()
        {
            return from DriverHRStatus in db.DriverHRStatuses
                   orderby DriverHRStatus.DriverHRStatusCode descending
                   select DriverHRStatus;
        }


        public IQueryable<DriverHRStatus> GetDataByName(string searchField, string searchOper, string searchString)
        {
            var ps = db.DriverHRStatuses.Where(p => p.DriverHRStatusCode.Contains(searchString));
            if (searchField == "DriverHRStatusDescription")
                ps = db.DriverHRStatuses.Where(p => p.DriverHRStatusDescription.Contains(searchString));
            if (searchField == "DriverHRStatusCode")
                ps = db.DriverHRStatuses.Where(p => p.DriverHRStatusCode.Contains(searchString));
            return ps.AsQueryable();

        }
        public IQueryable<DriverHRStatus> GetAllData(string sortColumn, string sortOrder, bool _search, string searchField, string searchOper, string searchString)
        {
            if (_search)
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetDataByName(searchField, searchOper, searchString).OrderByDescending(sortColumn);
                    else
                        return GetDataByName(searchField, searchOper, searchString).OrderBy(sortColumn);
                else
                    return GetDataByName(searchField, searchOper, searchString).OrderBy(sortColumn);
            }
            else
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetAllData().OrderByDescending(sortColumn);
                    else
                        return GetAllData().OrderBy(sortColumn);
                else
                    return GetAllData();
            }
        }



        public DriverHRStatus GetByID(string id)
        {
            return db.DriverHRStatuses.SingleOrDefault(DriverHRStatus => DriverHRStatus.DriverHRStatusCode == id);
        }

        //Inset
        public void Add(DriverHRStatus DriverHRStatus)
        {
            //if (DriverHRStatus.StatusID == 0 )
            //    DriverHRStatus.StatusID = 1;
            DriverHRStatus.InsertedUser = HttpContext.Current.User.Identity.Name;
            DriverHRStatus.InsertedDate = DateTime.Now;
            DriverHRStatus.UpdatedUser = HttpContext.Current.User.Identity.Name;
            DriverHRStatus.UpdatedDate = DateTime.Now;
            DriverHRStatus.DataVersion = 1;

            db.DriverHRStatuses.AddObject(DriverHRStatus);
        }

        //Update
        public void Update(DriverHRStatus DriverHRStatus)
        {

            DriverHRStatus originalDriverHRStatus = GetByID(DriverHRStatus.DriverHRStatusCode);

            DriverHRStatus.InsertedUser = originalDriverHRStatus.InsertedUser;
            DriverHRStatus.InsertedDate = originalDriverHRStatus.InsertedDate;
            DriverHRStatus.UpdatedUser = HttpContext.Current.User.Identity.Name;
            DriverHRStatus.UpdatedDate = DateTime.Now;
            DriverHRStatus.DataVersion = ++originalDriverHRStatus.DataVersion;
  
            db.DriverHRStatuses.ApplyCurrentValues(DriverHRStatus);
        }

        //Delete
        public void Delete(DriverHRStatus DriverHRStatus)
        {
            db.DriverHRStatuses.DeleteObject(DriverHRStatus);
        }

        //Persistance
        public void Save()
        {
            db.SaveChanges();
        }
    }
}